package cn.net.xyan.blossom.core.service


import cn.net.xyan.blossom.core.dao.LogDao
import cn.net.xyan.blossom.core.entity.AuditLog
import cn.net.xyan.blossom.kotlin.AllOpen
import cn.net.xyan.blossom.kotlin.json
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.Async

interface LogService{
    fun saveLog(log: AuditLog):AuditLog
    fun saveLog(type:String,content:String):AuditLog
    fun saveLog(type:String,content:String,clz:Class<*>,methodName:String):AuditLog
    fun saveLog(type:String,obj:Any?,clz:Class<*>,methodName:String):AuditLog
}

@AllOpen
class LogServiceImpl: LogService {


    @Autowired
    lateinit var logDao: LogDao

    @Async
    override fun saveLog(log: AuditLog): AuditLog {
        return logDao.saveAndFlush(log)
    }

    override fun saveLog(type: String, content: String): AuditLog {
        var log = AuditLog(type,content)
        return saveLog(log)
    }

    override fun saveLog(type: String, content: String, clz: Class<*>, methodName: String): AuditLog {
        var log = AuditLog(type,content)
        log.targetClassName = clz.name
        log.targetMethodName = methodName
        return saveLog(log)
    }

    override fun saveLog(type: String, obj: Any?, clz: Class<*>, methodName: String): AuditLog {
        return saveLog(type,obj?.json?:"NULL",clz,methodName)
    }


}